import { jsapi, viewUtils } from '@/utils/arcgis';

export async function exhydratGraphicLayer(layerId,projectId) {
  const view = await viewUtils.isViewReady();
  const sketchLayer = view.map.findLayerById(layerId);

  const graphics = sketchLayer.graphics.map(graphic => {
    return graphic.toJSON();
  });
  window.localStorage.setItem(layerId+projectId, JSON.stringify(graphics));
  return true;
}

export async function hydratGraphicLayer(layer,projectId) {
  let graphics = [];

  const [Graphic_] = await jsapi.load(['esri/Graphic']);
  try {
    // @ts-ignore
    const data = JSON.parse(window.localStorage.getItem(layer.id + projectId));
    graphics = data.map(graphicJSON => {
      return Graphic_.fromJSON(graphicJSON);
    });
    layer.addMany(graphics);
  } catch (error) {}

  return true;
}
